package net.adst.ui.mainframe.tabs.nanemTap;

import net.adst.beans.NaemTec;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.util.DataManager;
import net.adst.util.GBC;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 6:55
 * To change this template use File | Settings | File Templates.
 */
public class DetailedTableNaemPanel extends JPanel {

    private JTable detailedTable;

    public DetailedTableNaemPanel() {
        setLayout(new GridBagLayout());
        addDetTable();
    }

    private void addDetTable() {
        DefaultTableModel model = new DefaultTableModel( //NotEditableDefaultTableModel
                new Object[][]{
                },
                Constants.NAEM_НEADER
        ) {
            @Override
            public boolean isCellEditable(final int row, final int col) {
                return false;
            }
        };
        detailedTable = new JTable(model);
        detailedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        detailedTable.setRowHeight(Constants.TABLE_ROW_HEIGHT);
        detailedTable.getTableHeader().setReorderingAllowed(false);
        detailedTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting())
                    return;
                ListSelectionModel lsm = (ListSelectionModel) e.getSource();

                final JTextArea description = (JTextArea) DataManager.getObject(VariableNames.COMMENT_TEXT);

                if (lsm.isSelectionEmpty()) {
                    description.setText("");
                } else {
                    final JTable sumTable = (JTable) DataManager.getObject(VariableNames.UI_NAEM_SUM_TABLE);
                    sumTable.getSelectionModel().clearSelection();
                    final int selectedRow = lsm.getMinSelectionIndex();
                    DataManager.setObject(VariableNames.NAEM_SELECTED_ROW, (Integer) selectedRow);
                    JButton editButton = (JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_EDIT_BUTTON);
                    int col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[16]);
                    if (selectedRow > -1) {
                        if (detailedTable.getValueAt(selectedRow, col).equals("*")) {
                            ArrayList<NaemTec> rows = (ArrayList<NaemTec>) DataManager.getObject(VariableNames.TEC_NAEM_USING_ROWS);
                            description.setText(rows.get(selectedRow).getComment());
                            description.setEnabled(true);
                            description.setEditable(false);
                        } else {
                            description.setEnabled(false);
                            description.setEditable(false);
                            description.setText("");
                        }
                        editButton.setEnabled(true);
                    } else {
                        editButton.setEnabled(false);
                        return;
                    }
                }
            }
        });
        setWidth();
        JScrollPane scrollPane = new JScrollPane(detailedTable);
        DataManager.setObject(VariableNames.UI_NAEM_DETAILED_MODEL, model);
        DataManager.setObject(VariableNames.UI_NAEM_DETAILED_TABLE, detailedTable);
        add(scrollPane, new GBC(0, 0).setFill(GBC.BOTH).setWeight(1, 1));
    }

    private void setWidth() {
        // ORS-22
        int col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[0]);
        TableColumn column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[1]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(55);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[2]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(60);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[3]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(150);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[4]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[5]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(160);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[6]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(80);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[7]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(80);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[8]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[9]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(110);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[10]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[11]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[12]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[13]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[14]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(120);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[15]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[16]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
    }

}
